import React from 'react';
import './-page.scss';
import {observable, observer} from 'mobx.collection';
import {dateCountdownConvert, DateFormat} from "auxiliaries/extends/format";
import Avatar from "components/data/avatar";
import {inject, Provider} from "mobx-react";
import {DictController} from "api/dict";
import {REQUEST_STATE} from "fetchState";
import {notify} from "components/state/toast";

let IndustryList = observable([]);
(async () => {
    let {CODE, DATA, STATUS, MESSAGE, DETAILS} = await DictController.GetIndustryList();
    if (CODE === REQUEST_STATE.CANCEL) {
        return;
    }

    if (STATUS === 0) {
        IndustryList.clear();
        IndustryList.push(...DATA);
    } else {
        notify(DETAILS || MESSAGE);
    }
})();

@inject('list')
@observer
class InjectMessageComment extends React.Component {

    static formatDateToView(CreationTime) {
        let expiredCount = dateCountdownConvert(CreationTime, true);
        let result = DateFormat(CreationTime, 'HH:MM');

//        console.log(expiredCount);
        
        if (expiredCount.day > 0) {
            result = `${expiredCount.day}天前`;
            // if (expiredCount.hour > 0) {
            //     result += `${expiredCount.hour}小时前`;
            // } else {
            //     result += '前';
            // }
        } else if (expiredCount.hour > 0) {
            result = `${expiredCount.hour}小时前`;
            // if (expiredCount.minute > 0) {
            //     result += `${expiredCount.minute}分钟前`;
            // } else {
            //     result += '前';
            // }
        } else if (expiredCount.minute > 0) {
            result = `${expiredCount.minute}分钟前`;
        } else if (expiredCount.second > 0) {
            result = `${expiredCount.second}秒前`;
        }
        return result;
    }

    render() {
        let {
            item = {}
        } = this.props;
        let {
            ImgUrl,
            TrueName,
            CreationTime,
            OrgName,
            Industry,
            Content,
            // isVip//字段瞎鸡儿取的
        } = item;

        let IndustryName = this.props.list?.find(x => x.Id === Industry)?.Name;

        return (
            <div className={"--message-comment"}>
                <div className="info">
                    <Avatar src={ImgUrl}/>
                    <div className="-head">
                        <span className="name font-size-28">{TrueName}</span>
                        <div className="-worker font-size-24">
                            <span>{OrgName}</span>
                            <span>&nbsp;{IndustryName}</span>
                        </div>
                    </div>
                    <div className="time font-size-24">
                        <span>{InjectMessageComment.formatDateToView(CreationTime)}</span>
                    </div>
                </div>
                <div className="-comment-data font-size-26">
                    <article>{Content || ' '}</article>
                </div>
            </div>
        );
    }
}

export default function MessageComment(props) {
    return (
        <Provider list={IndustryList}>
            <InjectMessageComment {...props} list={IndustryList}/>
        </Provider>
    )
};